﻿using System.Collections.Generic;
using System.Linq;

namespace BUS.DAL
{
    public class SanPhamDAL
    {
        internal static List<SanPhamBUS> GetAll()
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         orderby p.ID descending
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,
                             SoTon = (int)p.SoTon,
                         };

            return result.ToList<SanPhamBUS>();
        }


        internal static int Insert(SanPhamBUS sanPhamBUS)
        {
            var db = DataAccess.Database;

            var sp = new SanPham()
            {
                MaSanPham = sanPhamBUS.MaSanPham,
                TenSanPham = sanPhamBUS.TenSanPham,
                IDLoaiSanPham = sanPhamBUS.LoaiSanPham.ID,
                MoTa = sanPhamBUS.MoTa,
                Gia = sanPhamBUS.Gia,
                Anh = sanPhamBUS.Anh,
                BaoHanh = sanPhamBUS.BaoHanh,
                SoTon=sanPhamBUS.SoTon,//dang
            };

            db.SanPhams.InsertOnSubmit(sp);
            db.SubmitChanges();
            return sp.ID;
        }

        internal static List<SanPhamBUS> GetByCate(int idCate)
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         where p.LoaiSanPham.ID == idCate
                         orderby p.ID descending 
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,
                             SoTon = (int)p.SoTon,
                         };

            return result.ToList<SanPhamBUS>();
        }

        internal static SanPhamBUS GetSanPhamByID(int id)
        {
            var db = DataAccess.Database;

            var result = from p in db.SanPhams
                         where p.ID == id
                         select new SanPhamBUS()
                         {
                             ID = p.ID,
                             MaSanPham = p.MaSanPham,
                             TenSanPham = p.TenSanPham,
                             LoaiSanPham = new LoaiSanPhamBUS()
                             {
                                 ID = p.LoaiSanPham.ID,
                                 TenLoai = p.LoaiSanPham.TenLoai
                             },
                             MoTa = p.MoTa,
                             Gia = (decimal)p.Gia,
                             Anh = p.Anh,
                             BaoHanh = p.BaoHanh,
                             SoTon = (int)p.SoTon,
                         };

            return result.FirstOrDefault<SanPhamBUS>();
        }

        internal static void Update(SanPhamBUS sanPhamBUS)
        {
            var db = DataAccess.Database;
            var sp = db.SanPhams.SingleOrDefault<SanPham>(p => p.ID == sanPhamBUS.ID);

            if (sp != null)
            {
                sp.MaSanPham = sanPhamBUS.MaSanPham;
                sp.TenSanPham = sanPhamBUS.TenSanPham;
                sp.IDLoaiSanPham = sanPhamBUS.LoaiSanPham.ID;
                sp.Gia = sanPhamBUS.Gia;
                sp.BaoHanh = sanPhamBUS.BaoHanh;
                sp.Anh = sanPhamBUS.Anh;
                sp.SoTon = sanPhamBUS.SoTon;//d
                sp.MoTa = sanPhamBUS.MoTa;//d
            }

            db.SubmitChanges();

        }

        internal static void Delete(SanPhamBUS sanPhamBUS)
        {
            ShopDataContext db = DataAccess.Database;

            var result = db.SanPhams.SingleOrDefault<SanPham>(p => p.ID == sanPhamBUS.ID);

            if (result != null) db.SanPhams.DeleteOnSubmit(result);
            db.SubmitChanges();
        }

        internal static List<SanPhamBUS> GetTopProducts()
        {
            var db = DataAccess.Database;

            var result = (from p in db.SanPhams
                          select new SanPhamBUS()
                          {
                              ID = p.ID,
                              MaSanPham = p.MaSanPham,
                              TenSanPham = p.TenSanPham,
                              LoaiSanPham = new LoaiSanPhamBUS() { TenLoai = p.LoaiSanPham.TenLoai },
                              Gia = p.Gia ?? 0,
                              Anh = p.Anh,
                              SoTon = p.SoTon ?? 0,
                              BaoHanh = p.BaoHanh,
                              SoLuong = p.ChiTietDatHangs.Sum(o => o.SoLuong) ?? 0
                          }).OrderByDescending(sp => sp.SoLuong).Take(6);

            return result.ToList<SanPhamBUS>();
        }

    }
}
